package com.multipleimageselect.adapters;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.asman.base.lib.glide.GlideApp;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jph.takephoto.R;
import com.multipleimageselect.activities.ImageSelectActivity;
import com.multipleimageselect.helpers.Constants;
import com.multipleimageselect.models.Album;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by Darshan on 4/14/2015.
 */
public class CustomAlbumSelectAdapter extends RecyclerView.Adapter<CustomAlbumSelectAdapter.MyViewHolder> {

    private ArrayList<Album> albums;

    public CustomAlbumSelectAdapter(ArrayList<Album> albums) {
        this.albums = albums;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_item_album_select, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.setData(albums.get(position));
    }

    @Override
    public int getItemCount() {
        return albums.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView image_view_album_image;
        TextView text_view_album_name;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            image_view_album_image = itemView.findViewById(R.id.image_view_album_image);
            text_view_album_name = itemView.findViewById(R.id.text_view_album_name);
        }

        public void setData(Album item) {
            itemView.setOnClickListener(v -> {
                Intent intent = new Intent(itemView.getContext(), ImageSelectActivity.class);
                intent.putExtra(Constants.INTENT_EXTRA_ALBUM, item.name);
                ((Activity) itemView.getContext()).startActivityForResult(intent, Constants.REQUEST_CODE);
            });
            text_view_album_name.setText(item.name);
            GlideApp.with(itemView.getContext())
                    .load(new File(item.cover))
                    .apply(new RequestOptions().centerCrop().placeholder(new ColorDrawable(Color.parseColor("#f7f7f7"))).diskCacheStrategy(DiskCacheStrategy.NONE))
                    .into(image_view_album_image);
        }
    }
}